package com.jd.order.service.impl;

import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.LoginContext;
import com.jd.basic.util.PageList;
import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.dto.OrderDto;
import com.jd.order.mapper.OrderPetAcquisitionMapper;
import com.jd.order.query.OrderPetAcquisitionQuery;
import com.jd.order.service.OrderPetAcquisitionService;
import com.jd.org.domain.Employee;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.mapper.ShopMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import com.jd.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "order_topic", consumerGroup = "my-order-tag")
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements OrderPetAcquisitionService, RocketMQListener<OrderPetAcquisition> {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;


    @Override
    @Transactional
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        // 获取登录信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id()!=null) { // 店铺管理员【店长或者店员】
            // 把shop_id设置进去
            query.setShopId(employee.getShop_id());
            // 获取店铺的信息
            Shop shop = shopMapper.loadById(employee.getShop_id());
            // 店员
            if (shop.getAdminId()!=employee.getId()) {
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }


    @Override
    @Transactional
    public void cancel(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        // 1.shopId置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        // 2.删除宠物信息，订单取消了，宠物就没有接回来。之前已经将宠物录入系统了
        petMapper.del(order.getPetId());

        // 3.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);

    }

    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        // 1.改订单状态  0待处理  1已处理
        // 2.修改订单的支付方式和金额
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderDto.getId());
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        orderPetAcquisitionMapper.update(order);

        // 3.修改寻主消息状态  1待处理  2已完成
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        // 4.修改pet的成本价
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);


    }


    @Override
    public void onMessage(OrderPetAcquisition orderPetAcquisition) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderPetAcquisition.getId());
        log.info("收到延迟信息："+order);
        if (order.getState() == 0) {
            // 1.shopId置空
            searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

            // 2.删除宠物信息，订单取消了，宠物就没有接回来。之前已经将宠物录入系统了
            petMapper.del(order.getPetId());

            // 3.改状态
            order.setState(-1);
            orderPetAcquisitionMapper.update(order);

        }
    }
}
